home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / pt20pc.zip / EXTEND.C < prev    next >
C/C++ Source or Header  |  1991-02-04  |  4KB  |  165 lines

  1. #include "pt.h"
  2.  
  3. /* return 1 only if you see both buttons simultaneously down */
  4. int pascal
  5. /* XTAG:extend */
  6. extend(w, cp,  anchorRow, anchorCol, evhead)
  7.     register struct window *w;
  8.     long cp;
  9.     int anchorRow, anchorCol, evhead;
  10. {
  11.     extern unsigned char msgBuffer[];
  12.     extern unsigned char textBuffer[];
  13.     extern union REGS rin, rout;
  14.     extern struct window *selWindow;
  15.     extern long selBegin, selEnd;
  16.     extern int selMode;
  17.     extern unsigned char scrMapReset;
  18.     extern struct window *pendWindow;
  19.     extern int debug;
  20.  
  21.     int ret, n, row1, col1, col2, minRow, maxRow;
  22.     long cp2, cp3, sb, limit;
  23.  
  24.     /* can only extend in the selection window */
  25.     if( w != selWindow ) {
  26.         return 0;
  27.     }
  28.  
  29.     /* set the screen map for the visible parts of this window */
  30.     setMap(w->row1, w->col1, w->row2, w->col2, 1, 0x07);
  31.     maskTop(w);
  32.  
  33.     /* for now do not reset the screenMap when you write a char */
  34.     /* this avoids constant recomputation of the screenMap */
  35.     scrMapReset = 1;
  36.  
  37.     /* erase the selection if any part of it is in this window */
  38.     if(  selBegin <= w->posBotline && selEnd >= w->posTopline ) {
  39.         sb = selBegin;
  40.         selBegin = selEnd + 1;
  41.         row1 = w->row1 + 1;
  42.         col1 = w->col1 + 1;
  43.         col2 = w->col2 - 1;
  44.         if( selEnd <= w->posBotline )
  45.             limit = selEnd + 1;
  46.         else
  47.             limit = w->posBotline;
  48.         cp2 = w->posTopline;
  49.         minRow = maxRow = -1;
  50.         while( cp2 < limit ) {
  51.             n = 1;
  52.             cp3 = nextLine(w->fileId, cp2, &n);
  53.             if( sb <= cp3 ) {
  54.                 cp3 = fillLine(w, cp2, row1, col1, col2);
  55.                 if( minRow == -1 )
  56.                     minRow = row1;
  57.                 maxRow = row1;
  58.             }
  59.             cp2 = cp3;
  60.             ++row1;
  61.             if( row1 >= w->row2 || cp3 == -1 )
  62.                 break;
  63.         }
  64. /*****
  65.         if( maxRow != -1 )
  66.             updateScreen(minRow, maxRow);
  67. *****/
  68.         selBegin = sb;
  69.     }
  70.  
  71.     /* reset the selection for extension or contraction */
  72.     /* reduce the selection to one selMode unit */
  73.     /* only do it if we are not extending backwards */
  74.     if( cp >= selBegin )
  75.         modeExtend(w, selBegin, &cp2, &selEnd);
  76.  
  77.     /* as long as the button is down, follow the selection */
  78.     if( evhead != -1 ) {    /* only "follow" for regular selections */
  79.         ret = followSelection(w, anchorRow, anchorCol, evhead, 1);
  80.     }
  81.  
  82.     /* selecting the end of line should include both CR and LF */
  83.     if( readChar(w->fileId, selBegin) == '\n' ) {
  84.         if( readChar(w->fileId, --selBegin) != '\r' )
  85.             ++selBegin;
  86.     }
  87.  
  88.     /* restore the correct screen map reset value  */
  89.     scrMapReset = 0;
  90.  
  91.     return ret;
  92. }
  93.  
  94. void pascal
  95. /* XTAG:modeExtend */
  96. modeExtend(w, cp, begin, end)
  97.     struct window *w;
  98.     long cp, *begin, *end;
  99. {
  100.     extern int selMode;
  101.  
  102.     unsigned char ch;
  103.     int n, fid;
  104.     long cpLine, cpLow, cpHigh;
  105.  
  106.     /* find the beginning of the line */
  107.     n = -1;
  108.     cpLine = prevLine(w->fileId, cp, &n);
  109.     
  110.     fid = w->fileId;
  111.  
  112.     /* determine the initial selection */
  113.     switch( selMode ) {
  114.  
  115.     case SELCHAR:
  116.         *end = *begin = cp;
  117.         ch = readChar(fid, cp);
  118.         if( ch == '\r' ) {
  119.             ch = readChar(fid, cp+1);
  120.             if( ch == '\n' )
  121.                 ++(*end);
  122.         }
  123.         break;
  124.  
  125.     case SELWORD:
  126.         cpLow = cp;
  127.         while( 1 ) {
  128.             ch = readChar(fid, cpLow);
  129.             if( cpLow <= cpLine || (!isalnum(ch) && ch != '_') )
  130.                 break;
  131.             --cpLow;
  132.         }
  133.         if( !isalnum(ch) && ch != '_' && cpLow < cp )
  134.             ++cpLow;
  135.         cpHigh = cp;
  136.         while( 1 ) {
  137.             ch = readChar(fid, cpHigh);
  138.             if( !isalnum(ch) && ch != '_' )
  139.                 break;
  140.             ++cpHigh;
  141.         }
  142.         if( cpHigh > cp )
  143.             --cpHigh;
  144.         if( cpLow == cpHigh && readChar(fid, cpLow) == '\r'
  145.                     && readChar(fid, cpLow+1) == '\n' )
  146.              ++cpHigh;
  147.         *begin = cpLow;
  148.         *end = cpHigh;
  149.         break;
  150.  
  151.     case SELLINE:
  152.         *begin = cpLine;
  153.         n = 1;
  154.         *end = nextLine(w->fileId, cpLine, &n) - 1;
  155.         if( *end < *begin )
  156.             *end = *begin;
  157.         break;
  158.  
  159.     case SELALL:
  160.         *begin = 0;
  161.         *end = fileSize(w->fileId) - 1;
  162.         break;
  163.     }
  164. }
  165.